/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.xml;

/**
 *
 * @author Ugo
 */

import it.polimi.se2.meteocal.entity.Calendar;
import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.Participation;
import it.polimi.se2.meteocal.entity.Users;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;




 
public class JAXBXMLHandler {
 
    // Export
    private static FileWriter filewriter;
    private static BufferedWriter buffwriter;
    
        
     public static void marshalUser(Users user,Calendar calendar, List<Event> events, List<Participation> participations,File selectedFile) throws IOException, JAXBException {
        filewriter=new FileWriter(selectedFile);
        buffwriter = new BufferedWriter(filewriter);
        JAXBContext context;
        
        context = JAXBContext.newInstance(Users.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(user, buffwriter);
        
        context = JAXBContext.newInstance(Calendar.class);
        m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(calendar, buffwriter);
        
        context = JAXBContext.newInstance(Events.class);
        m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(new Events(events), buffwriter);
        
        context = JAXBContext.newInstance(Participations.class);
        m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(new Participations(participations), buffwriter);
        buffwriter.close();
    }
          
    // Import
    
     public static Users unmarshalUser(InputStream is) throws IOException, JAXBException {
        JAXBContext context;
        context = JAXBContext.newInstance(Users.class);
        Unmarshaller um = context.createUnmarshaller();
        Users user = (Users) um.unmarshal(is);
        return user;
    }
     
    public static Calendar unmarshalCalendar(InputStream is) throws IOException, JAXBException {
        JAXBContext context;
        context = JAXBContext.newInstance(Calendar.class);
        Unmarshaller um = context.createUnmarshaller();
        Calendar calendar = (Calendar) um.unmarshal(is);
        return calendar;
    }
      
    public static List<Event> unmarshalEvents(InputStream is) throws JAXBException {
        Events events = new Events();
    
        JAXBContext context = JAXBContext.newInstance(Events.class);
        Unmarshaller um = context.createUnmarshaller();
        events = (Events) um.unmarshal(is);
 
        return events.getEvents();
    }
       
    public static List<Participation> unmarshalParticipations(InputStream is) throws JAXBException {
        Participations participations = new Participations();
        
        JAXBContext context = JAXBContext.newInstance(Participations.class);
        Unmarshaller um = context.createUnmarshaller();
        participations = (Participations) um.unmarshal(is);
 
        return participations.getParticipations();
    }
         
}

